function getGsProductJs() {
    let shop = document.getElementById("shop")
    let area = document.getElementById("area")
    let shopStr = document.getElementById("shopStr")
    let areaStr = document.getElementById("areaStr")
    let shopNavBox = document.getElementById("shopNavBox")
    let areaNavBox = document.getElementById("areaNavBox")
    let main = document.getElementById("main")
        // 初始化商铺号和区域号
    let shopNum = 0;
    let areaNum = 0;
    // 渲染商铺列表函数
    function renderShopNav(shopData) {
        let html = ''
        shopData.forEach(item => {
            html += `<div>${item.shopName}</div>`
        });
        shopNavBox.innerHTML = html

    }
    // 渲染区域列表函数
    function renderAreaNav(areaData) {
        let html = ''
        areaData.forEach(item => {
            html += `<div>${item.areaName}</div>`
        });
        areaNavBox.innerHTML = html;
    }
    // 渲染凑单页面函数
    function renderPageHtml(pageData) {
        let html = ""
        pageData.forEach(item => {
            html += `
        <div>
        <img src="${item.productImg}">
        <span>${item.productName}</span>
        <div>
            <span>${item.productPrice}</span>
            <a href="">去凑单</a>
        </div>
        </div>`
        })
        main.innerHTML = html
    }
    // 申请凑单页面数据函数
    function renderPage(shopNum, areaNum) {
        ajax({
            url: "http://chst.vip:1234/api/getgsproduct?shopid=" + shopNum + "&areaid=" + areaNum + "",
            success(res) {
                let pageData = res.result;
                renderPageHtml(pageData)
            }
        })
    }
    renderPage(shopNum, areaNum)
        // 请求商铺信息
    ajax({
            url: "http://chst.vip:1234/api/getgsshop?key=all",
            success(res) {
                let shopData = res.result;
                // 渲染商铺列表
                renderShopNav(shopData);
                // 获取列表
                let shopNavData = shopNavBox.children;
                console.log(shopNavData);
                let flag = true
                shop.onclick = function() {
                    if (flag) {
                        shopNavBox.style.display = 'block';
                        flag = !flag
                    } else {
                        shopNavBox.style.display = 'none';
                        flag = !flag

                    }
                    for (let i = 0; i < shopNavData.length; i++) {
                        shopNavData[i].onclick = function() {
                            console.log(shopData[i].shopName);
                            console.log(shopData[i].shopId);
                            shopStr.innerText = shopData[i].shopName
                            shopNavBox.style.display = 'none';
                            let shopNum = shopData[i].shopId
                            renderPage(shopNum, areaNum)
                        }
                    }
                }

            }
        })
        // 请求区域信息
    ajax({
            url: "http://chst.vip:1234/api/getgsshoparea?key=all",
            success(res) {
                let areaData = res.result;
                renderAreaNav(areaData)
                    // 获取列表
                let areaNavData = areaNavBox.children;
                console.log(areaNavData);
                let flag = true
                area.onclick = function() {
                    if (flag) {
                        areaNavBox.style.display = 'block';
                        flag = !flag
                    } else {
                        areaNavBox.style.display = 'none';
                        flag = !flag

                    }
                    for (let i = 0; i < areaNavData.length; i++) {
                        areaNavData[i].onclick = function() {
                            console.log(areaData[i].areaName);
                            console.log(areaData[i].areaId);
                            areaStr.innerText = areaData[i].areaName.substr(0, 2)
                            areaNavBox.style.display = 'none';
                            let areaNum = areaData[i].areaId
                            renderPage(shopNum, areaNum)

                        }
                    }
                }
            }
        })
        // 返回顶部按钮功能实现
    document.querySelector('#main').onscroll = function() {
        top0.onclick = function() {
            document.querySelector('#main').scrollTop = 0
        }
    }
}
getGsProductJs()